﻿using System;
using System.Collections.Generic;
using System.Data;
using System.Data.Entity;
using System.Linq;
using System.Web;
using System.Web.Mvc;
using CloudFranceTriple.Models;

namespace CloudFranceTriple.WebRole.Areas.Forum.Controllers
{
    [CloudFranceTriple.WebRole.Filters.InitializeSimpleMembership]
    public class AnnonceController : Controller
    {
        private cloudfrAboq2RsDMEntities db = new cloudfrAboq2RsDMEntities();

        //
        // GET: /Forum/Annonce/ShowOnHeader

        [ChildActionOnly]
        [AllowAnonymous()]
        [Authorize(Roles = "RoleMember,RoleFans,RoleBaffMember,RoleAdminAnnonce")]
        public ActionResult ShowOnHeader()
        {
            var _now = DateTime.UtcNow;
            var _yesterday = _now.AddDays(-1);
            return PartialView("_AnnonceShow", db.ForumAnnonceSet.Where(an1 => an1.ShowOnHeader).Where(an2 => an2.DateBegin <= _now).Where(an3 => (an3.DateEnd == null) || (an3.DateEnd >= _yesterday)).OrderBy(annonce => annonce.Order).ToList());
        }

        //
        // GET: /Forum/Annonce/ShowOnPortail

        [ChildActionOnly]
        [AllowAnonymous()]
        [Authorize(Roles = "RoleMember,RoleFans,RoleBaffMember,RoleAdminAnnonce")]
        public ActionResult ShowOnPortail()
        {
            var _now = DateTime.UtcNow;
            var _yesterday = _now.AddDays(-1);
            return PartialView("_AnnonceShow", db.ForumAnnonceSet.Where(an1 => an1.ShowOnPortail).Where(an2 => an2.DateBegin <= _now).Where(an3 => (an3.DateEnd == null) || (an3.DateEnd >= _yesterday)).OrderBy(annonce => annonce.Order).ToList());
        }

        //
        // GET: /Forum/Annonce/ShowOnPost

        [ChildActionOnly]
        [AllowAnonymous()]
        [Authorize(Roles = "RoleMember,RoleFans,RoleBaffMember,RoleAdminAnnonce")]
        public ActionResult ShowOnPost()
        {
            var _now = DateTime.UtcNow;
            var _yesterday = _now.AddDays(-1);
            return PartialView("_AnnonceShow", db.ForumAnnonceSet.Where(an1 => an1.ShowOnPost).Where(an2 => an2.DateBegin <= _now).Where(an3 => (an3.DateEnd == null) || (an3.DateEnd >= _yesterday)).OrderBy(annonce => annonce.Order).ToList());
        }

        //
        // GET: /Forum/Annonce/ShowOnTopic

        [ChildActionOnly]
        [AllowAnonymous()]
        [Authorize(Roles = "RoleMember,RoleFans,RoleBaffMember,RoleAdminAnnonce")]
        public ActionResult ShowOnTopic()
        {
            var _now = DateTime.UtcNow;
            var _yesterday = _now.AddDays(-1);
            return PartialView("_AnnonceShow", db.ForumAnnonceSet.Where(an1 => an1.ShowOnTopic).Where(an2 => an2.DateBegin <= _now).Where(an3 => (an3.DateEnd == null) || (an3.DateEnd >= _yesterday)).OrderBy(annonce => annonce.Order).ToList());
        }

        //
        // GET: /Forum/Annonce/ShowOnForum

        [ChildActionOnly]
        [AllowAnonymous()]
        [Authorize(Roles = "RoleMember,RoleFans,RoleBaffMember,RoleAdminAnnonce")]
        public ActionResult ShowOnForum()
        {
            var _now = DateTime.UtcNow;
            var _yesterday = _now.AddDays(-1);
            return PartialView("_AnnonceShow", db.ForumAnnonceSet.Where(an1 => an1.ShowOnForum).Where(an2 => an2.DateBegin <= _now).Where(an3 => (an3.DateEnd == null) || (an3.DateEnd >= _yesterday)).OrderBy(annonce => annonce.Order).ToList());
        }


        //
        // GET: /Forum/Annonce/

        [Authorize(Roles = "RoleAdminAnnonce")]
        public ActionResult Index()
        {
            return View(db.ForumAnnonceSet.ToList());
        }

        //
        // GET: /Forum/Annonce/Create

        [Authorize(Roles = "RoleAdminAnnonce")]
        public ActionResult Create()
        {
            return View();
        }

        //
        // POST: /Forum/Annonce/Create

        [HttpPost]
        [ValidateAntiForgeryToken]
        [Authorize(Roles = "RoleAdminAnnonce")]
        [ValidateInput(false)]
        public ActionResult Create(ForumAnnonceSet forumannonceset)
        {
            if (ModelState.IsValid)
            {
                db.ForumAnnonceSet.Add(forumannonceset);
                db.SaveChanges();
                return RedirectToAction("Index");
            }

            return View(forumannonceset);
        }

        //
        // GET: /Forum/Annonce/Edit/5

        [Authorize(Roles = "RoleAdminAnnonce")]
        public ActionResult Edit(int id = 0)
        {
            ForumAnnonceSet forumannonceset = db.ForumAnnonceSet.Find(id);
            if (forumannonceset == null)
            {
                return HttpNotFound();
            }
            return View(forumannonceset);
        }

        //
        // POST: /Forum/Annonce/Edit/5

        [HttpPost]
        [ValidateAntiForgeryToken]
        [Authorize(Roles = "RoleAdminAnnonce")]
        [ValidateInput(false)]
        public ActionResult Edit(ForumAnnonceSet forumannonceset)
        {
            if (ModelState.IsValid)
            {
                db.Entry(forumannonceset).State = EntityState.Modified;
                db.SaveChanges();
                return RedirectToAction("Index");
            }
            return View(forumannonceset);
        }

        //
        // GET: /Forum/Annonce/Delete/5

        [Authorize(Roles = "RoleAdminAnnonce")]
        public ActionResult Delete(int id = 0)
        {
            ForumAnnonceSet forumannonceset = db.ForumAnnonceSet.Find(id);
            if (forumannonceset == null)
            {
                return HttpNotFound();
            }
            return View(forumannonceset);
        }

        //
        // POST: /Forum/Annonce/Delete/5

        [HttpPost, ActionName("Delete")]
        [ValidateAntiForgeryToken]
        [Authorize(Roles = "RoleAdminAnnonce")]
        public ActionResult DeleteConfirmed(int id)
        {
            ForumAnnonceSet forumannonceset = db.ForumAnnonceSet.Find(id);
            db.ForumAnnonceSet.Remove(forumannonceset);
            db.SaveChanges();
            return RedirectToAction("Index");
        }

        protected override void Dispose(bool disposing)
        {
            db.Dispose();
            base.Dispose(disposing);
        }
    }
}